﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WebApi.Controllers
{
    /// <summary>
    /// 测试控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        ///// <summary>
        ///// 下面的方法默认使用 [FromForm]特性
        ///// [FromForm] 标记的复杂参数不能超过1个，否则会报错
        ///// </summary>
        ///// <param name="product"></param>
        ///// <param name="order"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public IActionResult ErrorProduct(Product product,Order order)
        //{
        //    return Ok(new { product,order });
        //}

        /// <summary>
        /// 测试表单提交
        /// </summary>
        /// <param name="formFile"></param>
        /// <returns></returns>
        [HttpPost("UpFile")]
        public IActionResult About(IFormFile formFile)
        {
            return Ok();
        }

        /// <summary>
        /// 测试返回问题详情
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ReturnProblemDetails()
        {
            return NotFound();
        }

        /// <summary>
        /// 测试返回问题详情
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        [HttpPost("PostJson")]
        [Consumes("application/json")]
        public IActionResult PostJson(IEnumerable<int> values )
            =>Ok(new {Consumes="application/json",values=values});

        /// <summary>
        /// 测试返回问题详情
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        [HttpPost]
        [Consumes("application/x-www-form-urlencoded")]
        public IActionResult PostForm([FromForm]IEnumerable<int> values)
            =>Ok(new {Consumes= "application/x-www-form-urlencoded",Values =values});

        /// <summary>
        /// 除法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        [HttpGet("{a:int}/{b:int}")]
        public IActionResult Divide(double a, double b)
        {
            if (b == 0)
            {
                return BadRequest("除数不能为0");
            }
            return Ok(a / b);
        }

        /// <summary>
        /// 圆的面积
        /// </summary>
        /// <param name="radicand"></param>
        /// <returns></returns>
        [HttpGet("{radicand:double}")]
        public IActionResult SquareRoot(double radicand)
        {
            if (radicand<0)
            {
                return BadRequest("半径不能为负数");
            }
            return Ok(Math.Sqrt(radicand));
        }

    }

    /// <summary>
    /// 产品模型
    /// </summary>
    public class Product
    {
        public int Id { get; set; }
        public string? Name { get; set; }
    }

    public class Order
    {
        public int Id { get; set; }
        public string? Name { get; set; }
        public int ProductId { get; set; }
    }
}
